package org.lt.cj.translate;

import java.util.List;
import org.lt.cj.config.ServerProperty;
import org.lt.cj.model.dao.ICollectDao;
import org.lt.cj.model.impl.CollectImpl;
import org.lt.cj.model.pojo.Attribute;
import org.lt.cj.model.pojo.Category;
import org.lt.cj.model.pojo.Description;
import org.lt.cj.model.pojo.Evaluation;
import org.lt.cj.model.pojo.Product;
import org.lt.cj.model.pojo.Seller;

/**
 * 翻译器类
 * 
 * @author zgh
 */
public class Translator {

    private ICollectDao cd = new CollectImpl();
    private List<Category> cList = null;
    private List<Seller> sList = null;
    private List<Product> pList = null;
    private List<Attribute> aList = null;
    private List<Evaluation> eList = null;
    private List<Description> dList = null;
    private static String from;
    private static String to;
    private static int cateCount = 50;
    private static int sellerCount;
    private static int productCount;
    private static int attributeCount;
    private static int evaluationCount;
    private static int descriptionCount;
    private static int cateTimes = 0;
    private static int sellerNameTimes = 0;
    private static int productTimes = 0;
    private static int attributeTimes = 0;
    private static int evaluationTimes;
    private static int descriptionTimes;
    private String[] cateName = null;
    private String[] sellerName = null;
    private String[] sellerCompany = null;
    private String[] sellerLocation = null;
    private String[] productName = null;
    private String[] attributeName = null;
    private String[] attributeValue = null;
    private String[] evaluationContent = null;
    private String[] descriptionContent = null;

    public Translator() {
    }

    static {
        ServerProperty sp = new ServerProperty();
        from = sp.getValue("languageFrom");
        to = sp.getValue("languageTo");

        cateCount = (sp.getValue("cateCount") != null && !sp.getValue(
                "cateCount").equals("")) ? Integer.parseInt(sp.getValue("cateCount")) : 50;
        sellerCount = (sp.getValue("sellerCount") != null && !sp.getValue(
                "sellerCount").equals("")) ? Integer.parseInt(sp.getValue("sellerCount")) : 100;
        productCount = (sp.getValue("productCount") != null && !sp.getValue(
                "productCount").equals("")) ? Integer.parseInt(sp.getValue("productCount")) : 100;
        attributeCount = (sp.getValue("attributeCount") != null && !sp.getValue(
                "attributeCount").equals("")) ? Integer.parseInt(sp.getValue("attributeCount")) : 100;
        evaluationCount = (sp.getValue("evaluationCount") != null && !sp.getValue(
                "evaluationCount").equals("")) ? Integer.parseInt(sp.getValue("evaluationCount")) : 100;
        descriptionCount = (sp.getValue("descriptionCount") != null && !sp.getValue(
                "descriptionCount").equals("")) ? Integer.parseInt(sp.getValue("descriptionCount")) : 100;
        sp.closeI();
    }

    // 查询分类
    public void queryCategory() {
        if (cList != null) {
            cList.removeAll(cList);
        }
        cList = cd.getCategory(from, cateCount);
        if (cList.size() > 0) {
            cateName = new String[cList.size()];
            // 设置分类数组
            for (int i = 0; i < cList.size(); i++) {
                cateName[i] = cList.get(i).getName();
            }
        } else {
            System.out.println("---------------category is empty.");
        }
    }

    //翻译分类并插入翻译后的对应语言表
    public void translateCategory() {
        String[] tCategory = null;
        if (cateName != null && cList.size() > 0) {

//	    tCategory = GoogleTranslate.translate(cateName, this.from,this.to); //google网页翻译
            tCategory = GoogleTranslate.googleApiTranslateArray(cateName,
                    from, to); // 谷歌API翻译
            System.out.println("categoryTranslated.size=" + tCategory.length);

            if (tCategory.length == cList.size()) { // 翻译前后数据量一样才入库
                for (int i = 0; i < cList.size(); i++) {
                    cd.insertCategory(cList.get(i).getId(), tCategory[i], cList.get(i).getFirstCate(), cList.get(i).getSecondCate(), to, from, 1);
                }

                markCategoryTranslated(); // 将问题标记为已翻译
                cateTimes = 0;
            } else {
                cateTimes++;
                if (cateTimes > 2) { // 翻译第三次出错
                    markCategoryTranslated(); // 将问题标记为已翻译,不再翻译
                    cateTimes = 0;
                }
            }
        }
    }

    // 将分类标记为已翻译
    public void markCategoryTranslated() {
        if (cList != null && cList.size() > 0) { // 非空
            for (Category c : cList) {
                cd.setCategoryTranslated(c.getId(), from, to);
            }
        } else {
            System.out.println("---------------分类为空，不用标记为已翻译。");
        }
    }

    // 查询商家信息
    public void querySeller() {
        if (sList != null) {
            sList.removeAll(sList);
        }
        sList = cd.getSeller(from, sellerCount);
        if (sList.size() > 0) {
            sellerName = new String[sList.size()];
            sellerCompany = new String[sList.size()];
            sellerLocation = new String[sList.size()];

            // 设置分类数组
            for (int i = 0; i < sList.size(); i++) {
                sellerName[i] = sList.get(i).getSellerName();
                sellerCompany[i] = sList.get(i).getCompany();
                sellerLocation[i] = sList.get(i).getLocation();
            }
        } else {
            System.out.println("---------------seller is empty.");
        }
    }

    //翻译问题并插入翻译后的对应语言表
    public void translateSeller() {
        String[] tSellerName = null;
        String[] tSellerCompany = null;
        String[] tSellerLocation = null;
        if (sellerName != null && sList.size() > 0) {
//	    tCategory = GoogleTranslate.translate(cateName, this.from,this.to); //google网页翻译
            tSellerName = GoogleTranslate.googleApiTranslateArray(sellerName,
                    from, to); // 谷歌API翻译
            tSellerCompany = GoogleTranslate.googleApiTranslateArray(sellerCompany,
                    from, to); // 谷歌API翻译
            tSellerLocation = GoogleTranslate.googleApiTranslateArray(sellerLocation,
                    from, to); // 谷歌API翻译

            System.out.println("sellerTranslated.size=" + tSellerName.length);
            if (tSellerName.length == sList.size()) { // 翻译前后数据量一样才入库
                for (int i = 0; i < sList.size(); i++) {
                    cd.insertSeller(sList.get(i).getId(), tSellerName[i], tSellerCompany[i], tSellerLocation[i], sList.get(i).getSite(), sList.get(i).getImNumber(), to, from, 1);

                }

                markSellerTranslated(); // 将商家信息标记为已翻译
                sellerNameTimes = 0;
            } else {
                sellerNameTimes++;
                if (sellerNameTimes > 2) { // 翻译第三次出错
                    markSellerTranslated(); // 将商家信息标记为已翻译,不再翻译
                    sellerNameTimes = 0;
                }
            }
        }
    }

    // 将商家标记为已翻译
    public void markSellerTranslated() {
        if (sList != null && sList.size() > 0) { // 非空
            for (Seller s : sList) {
                cd.setSellerTranslated(s.getId(), from, to);
            }
        } else {
            System.out.println("---------------分类为空，不用标记为已翻译。");
        }
    }

    // 查询商品
    public void queryProduct() {
        pList = cd.getProduct(from, productCount);
        if (pList.size() > 0) {
            productName = new String[pList.size()];
            // 设置分类数组
            for (int i = 0; i < pList.size(); i++) {
                productName[i] = pList.get(i).getName();
            }
        } else {
            System.out.println("---------------product is empty.");
        }
    }

    //翻译商品并插入翻译后的对应语言表
    public void translateProduct() {
        String[] tProductName = null;
        if (productName != null && pList.size() > 0) {

//	    tProductName = GoogleTranslate.translate(productName, this.from,this.to); //google网页翻译
            tProductName = GoogleTranslate.googleApiTranslateArray(productName,
                    from, to); // 谷歌API翻译
            System.out.println("productTranslated.size=" + tProductName.length);

            if (tProductName.length == pList.size()) { // 翻译前后数据量一样才入库
                for (int i = 0; i < pList.size(); i++) {
                    cd.insertProduct(pList.get(i).getId(), pList.get(i).getCateid(), pList.get(i).getSellerid(), tProductName[i], pList.get(i).getPrice(), pList.get(i).getSellCount(), pList.get(i).getCollectTime(), pList.get(i).getUrl(), to, from, 1);
                }

                markProductTranslated(); // 将问题标记为已翻译
                productTimes = 0;
            } else {
                productTimes++;
                if (productTimes > 2) { // 翻译第三次出错
                    markProductTranslated(); // 将问题标记为已翻译,不再翻译
                    productTimes = 0;
                }
            }
        }
    }

    // 将商品标记为已翻译
    public void markProductTranslated() {
        if (pList != null && pList.size() > 0) { // 非空
            for (Product p : pList) {
                cd.setProductTranslated(p.getId(), from, to);
            }
        } else {
            System.out.println("---------------商品为空，不用标记为已翻译。");
        }
    }

    // 查询属性
    public void queryAttribute() {
        if (aList != null) {
            aList.removeAll(aList);
        }
        aList = cd.getAttribute(from, attributeCount);
        if (aList.size() > 0) {
            attributeName = new String[aList.size()];
            attributeValue = new String[aList.size()];
            // 设置分类数组
            for (int i = 0; i < aList.size(); i++) {
                attributeName[i] = aList.get(i).getName();
                attributeValue[i] = aList.get(i).getValue();
            }
        } else {
            System.out.println("---------------attribute is empty.");
        }
    }

    //翻译商品并插入翻译后的对应语言表
    public void translateAttribute() {
        String[] tAttributeName = null;
        String[] tAttributeValue = null;
        if (attributeName != null && aList.size() > 0) {

//	    tAttributeName = GoogleTranslate.translate(attributeName, this.from,this.to); //google网页翻译
            tAttributeName = GoogleTranslate.googleApiTranslateArray(attributeName,
                    from, to); // 谷歌API翻译
            tAttributeValue = GoogleTranslate.googleApiTranslateArray(attributeValue,
                    from, to); // 谷歌API翻译
            System.out.println("attributeTranslated.size=" + tAttributeName.length);

            if (tAttributeName.length == aList.size()) { // 翻译前后数据量一样才入库
                for (int i = 0; i < aList.size(); i++) {
                    cd.insertAttribute(aList.get(i).getId(), aList.get(i).getProductid(), tAttributeName[i], tAttributeValue[i], to, from, 1);
                }

                markAttributeTranslated(); // 将属性标记为已翻译
                attributeTimes = 0;
            } else {
                attributeTimes++;
                if (attributeTimes > 2) { // 翻译第三次出错
                    markAttributeTranslated(); // 将属性标记为已翻译,不再翻译
                    attributeTimes = 0;
                }
            }
        }
    }

    // 将属性标记为已翻译
    public void markAttributeTranslated() {
        if (aList != null && aList.size() > 0) { // 非空
            for (Attribute a : aList) {
                cd.setAttrebuteTranslated(a.getId(), from, to);
            }
        } else {
            System.out.println("---------------属性为空，不用标记为已翻译。");
        }
    }

    // 查询评价
    public void queryEvaluation() {
        if (eList != null) {
            eList.removeAll(eList);
        }
        eList = cd.getEvaluation(from, evaluationCount);
        if (eList.size() > 0) {
            evaluationContent = new String[eList.size()];
            // 设置分类数组
            for (int i = 0; i < eList.size(); i++) {
                evaluationContent[i] = eList.get(i).getContent();
            }
        } else {
            System.out.println("---------------evaluation is empty.");
        }
    }

    //翻译商品并插入翻译后的对应语言表
    public void translateEvaluation() {
        String[] tEvaluationContent = null;
        if (evaluationContent != null && eList.size() > 0) {

//	    tAttributeName = GoogleTranslate.translate(attributeName, this.from,this.to); //google网页翻译
            tEvaluationContent = GoogleTranslate.googleApiTranslateArray(evaluationContent,
                    from, to); // 谷歌API翻译
            System.out.println("evaluationTranslated.size=" + tEvaluationContent.length);

            if (tEvaluationContent.length == eList.size()) { // 翻译前后数据量一样才入库
                for (int i = 0; i < eList.size(); i++) {
                    cd.insertEvaluation(eList.get(i).getId(), eList.get(i).getProductid(), tEvaluationContent[i], eList.get(i).getTime(), to, from, 1);
                }

                markEvaluationTranslated(); // 将评价标记为已翻译
                evaluationTimes = 0;
            } else {
                evaluationTimes++;
                if (evaluationTimes > 2) { // 翻译第三次出错
                    markEvaluationTranslated(); // 将评价标记为已翻译,不再翻译
                    evaluationTimes = 0;
                }
            }
        }
    }

    // 将评价标记为已翻译
    public void markEvaluationTranslated() {
        if (eList != null && eList.size() > 0) { // 非空
            for (Evaluation e : eList) {
                cd.setEvaluationTranslated(e.getId(), from, to);
            }
        } else {
            System.out.println("---------------评价为空，不用标记为已翻译。");
        }
    }

    // 查询评价
    public void queryDescription() {
        if (eList != null) {
            eList.removeAll(eList);
        }
        eList = cd.getEvaluation(from, evaluationCount);
        if (eList.size() > 0) {
            evaluationContent = new String[eList.size()];
            // 设置分类数组
            for (int i = 0; i < eList.size(); i++) {
                evaluationContent[i] = eList.get(i).getContent();
            }
        } else {
            System.out.println("---------------evaluation is empty.");
        }
    }

    //翻译商品并插入翻译后的对应语言表
    public void translateDescription() {
        String[] tEvaluationContent = null;
        if (evaluationContent != null && eList.size() > 0) {

//	    tAttributeName = GoogleTranslate.translate(attributeName, this.from,this.to); //google网页翻译
            tEvaluationContent = GoogleTranslate.googleApiTranslateArray(evaluationContent,
                    from, to); // 谷歌API翻译

            GoogleTranslate.googleApiTranslate("", from, to);
            System.out.println("evaluationTranslated.size=" + tEvaluationContent.length);

            if (tEvaluationContent.length == eList.size()) { // 翻译前后数据量一样才入库
                for (int i = 0; i < eList.size(); i++) {
                    cd.insertEvaluation(eList.get(i).getId(), eList.get(i).getProductid(), tEvaluationContent[i], eList.get(i).getTime(), to, from, 1);
                }

                markEvaluationTranslated(); // 将评价标记为已翻译
                evaluationTimes = 0;
            } else {
                evaluationTimes++;
                if (evaluationTimes > 2) { // 翻译第三次出错
                    markEvaluationTranslated(); // 将评价标记为已翻译,不再翻译
                    evaluationTimes = 0;
                }
            }
        }
    }

    // 将评价标记为已翻译
    public void markDescriptionTranslated() {
        if (eList != null && eList.size() > 0) { // 非空
            for (Evaluation e : eList) {
                cd.setEvaluationTranslated(e.getId(), from, to);
            }
        } else {
            System.out.println("---------------评价为空，不用标记为已翻译。");
        }
    }

    // 总调用翻译
    public void translateGo() {
        // 翻译分类
        queryCategory();
        translateCategory();

        //翻译商家信息，不用翻译
//        querySeller();
//        translateSeller();

        //翻译商品信息
        queryProduct();
        translateProduct();

        //翻译属性信息
        queryAttribute();
        translateAttribute();

        //翻译评价信息
        queryEvaluation();
        translateEvaluation();
    }

    public static void main(String[] args) {
        Translator tran = new Translator();
        // tran.translateGo();
        // System.out.println("from=" + tran.from + " to=" + tran.to
        // + " questionCount=" + tran.questionCount + " answerCount="
        // + tran.answerCount);
        //tran.translateGo();

        // 翻译分类
//        tran.queryCategory();
//        tran.translateCategory();

        //翻译商家信息
//        tran.querySeller();
//        tran.translateSeller();

        //翻译商品信息
//        tran.queryProduct();
//        tran.translateProduct();

        //翻译属性信息
//        tran.queryAttribute();
//        tran.translateAttribute();

        //翻译评价信息
//        tran.queryEvaluation();
//        tran.translateEvaluation();

        //翻译全部
        tran.translateGo();

    }
}
